<template>
  <div class="app-container">
    <el-card>
      <el-form
          :model="queryParams"
          ref="queryRef"
          :inline="true"
          v-show="showSearch"
      >
        <el-form-item label="服务项目">
          <el-input
              v-model="queryParams.name"
              placeholder="请输入服务项目"
              clearable
              @keyup.enter="handleQuery"
          />
        </el-form-item>

        <el-form-item label="项目类型">
          <el-select
              v-model="queryParams.type"
              clearable
              placeholder="请选择项目类型"
              style="width: 240px"
          >
            <el-option
                v-for="dict in project_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-select
              v-model="queryParams.status"
              clearable
              placeholder="请选择状态"
              style="width: 240px"
          >
            <el-option
                v-for="dict in contract_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
          >
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="Plus" @click="addDeviceTypes"
          >新增</el-button
          >
        </el-col>

        <right-toolbar
            v-model:showSearch="showSearch"
            @queryTable="getList"
        ></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="postList" stripe border>
        <el-table-column label="服务项目" align="center" prop="name" />
        <el-table-column label="项目类型" align="center" >
          <template #default="scope">
        <span>
          {{project_type.find(item=>item.value==scope.row.type)? project_type.find(item=>item.value==scope.row.type).label:''}}

        </span>
          </template>
        </el-table-column>
        <el-table-column label="服务内容" align="center" prop="content" show-overflow-tooltip width="500" />

        <el-table-column label="单价（元）" align="center" prop="unitPrice" />
        <el-table-column label="单位" align="center" >
          <template #default="scope">
        <span>
          {{bed_uint.find(item=>item.value==scope.row.unit)? bed_uint.find(item=>item.value==scope.row.unit).label:''}}

        </span>
          </template>
        </el-table-column>
        <el-table-column label="社区管家佣金（元）" align="center" prop="commission" />

        <el-table-column label="状态" align="center">
          <template #default="scope">
        <span>
          {{contract_status.find(item=>item.value==scope.row.status)? contract_status.find(item=>item.value==scope.row.status).label:''}}

        </span>
          </template>
        </el-table-column>

        <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
        >
          <template #default="scope">
            <el-button link type="primary" icon="Edit" @click="edit(scope.row)"
            >修改</el-button
            >
            <el-button
                link
                type="danger"
                icon="Delete"
                @click="handleDelete(scope.row)"
            >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="queryParams.page"
          v-model:limit="queryParams.limit"
          @pagination="getList"
      />
    </el-card>
    <el-dialog
        v-model="open"
        :title="title"
        append-to-body
        width="800px"
        v-if="open"
        @close="cancel"
        :close-on-click-modal="false"
    >

    </el-dialog>

    <!-- 添加或修改岗位对话框 -->
  </div>
</template>

<script setup name="apply">


const { proxy } = getCurrentInstance();
const { sys_normal_disable, sys_user_sex, device_category,project_type,contract_status,bed_uint} = proxy.useDict(
    "sys_normal_disable",
    "sys_user_sex",
    "device_category",
    "project_type",
    'contract_status',
    'bed_uint'
);
import {
  serviceItem, serviceItemSave, serviceItemUpdata,serviceItemDel
} from "@/api/homeBed/index";
import { debounce } from "@/utils/debounce.js";

const showSearch = ref(true);
const loading = ref(true);
const postList = ref([]);
const open = ref(false);
const title = ref("新增服务项目");

const total = ref(0);

const data = reactive({
  form: {},
  queryParams: {
    page: 1,
    limit: 10,
    status: undefined,
    type:undefined,
    name:undefined
  },
  rules: {

  },
});

const { queryParams, form, rules } = toRefs(data);

/** 提交按钮 */

function cancel() {
  open.value = false;
  reset();
}
function edit(data) {
  form.value = data;
  title.value = "编辑服务项目";
  open.value = true;
}

/** 查询岗位列表 */
function getList() {
  loading.value = true;
  serviceItem(queryParams.value).then((response) => {
    postList.value = response.page.list;
    total.value = response.page.totalCount;
    loading.value = false;
  });
}
function handleQuery() {
  getList();
}
function resetQuery() {
  queryParams.value = {
    page: 1,
    limit: 10,
    status: undefined,
    type:undefined,
    name:undefined
  };
  getList();
}

function handleDelete(row) {
  proxy.$modal
      .confirm("是否确认删除该数据项？")
      .then(function () {
        return serviceItemDel([row.id]);
      })
      .then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
      })
      .catch(() => {});
}
function addDeviceTypes() {
  title.value = "新增服务项目";
  open.value = true;
}

getList();
</script>
